package com.zqq.goods.ui.activity

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.zqq.baselibrary.ext.onClick
import com.zqq.baselibrary.ext.setVisible
import com.zqq.baselibrary.ui.activity.BaseActivity
import com.zqq.baselibrary.ui.adapter.BaseRecyclerViewAdapter
import com.zqq.baselibrary.utils.AppPrefsUtils
import com.zqq.goods.R
import com.zqq.goods.ui.adapter.SearchHistoryAdapter
import com.zqq.provider.router.RouterPath
import kotlinx.android.synthetic.main.activity_search_goods.*
import org.jetbrains.anko.toast

/**
 * 关键字搜索商品页
 * wc
 */
@Route(path = RouterPath.GoodsCenter.PATH_SEARCH_GOODS)
class SearchGoodsActivity : BaseActivity(), View.OnClickListener {

    private lateinit var mAdapter: SearchHistoryAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_search_goods)
        initView()
    }

    override fun onStart() {
        super.onStart()
        loadData()
    }

    private fun initView() {
        mLeftIv.onClick(this)
        mSearchTv.onClick(this)
        mClearBtn.onClick(this)

        //RecyclerView适配器
        mAdapter = SearchHistoryAdapter(this)
        mSearchHistoryRv.layoutManager = LinearLayoutManager(this)
        mSearchHistoryRv.adapter = mAdapter

        //item点击事件--方法一：
//        mAdapter.setOnItemClickListener(object: BaseRecyclerViewAdapter.OnItemClickListener<String> {
//            override fun onItemClick(item: String, position: Int) {
//
//            }
//        })

        mAdapter.mItemClickListener = object : BaseRecyclerViewAdapter.OnItemClickListener<String> {
            override fun onItemClick(item: String, position: Int) {
                enterGoodsList(item)
            }
        }
    }

    //从SP中加载 搜索历史数据
    private fun loadData() {
        val set = AppPrefsUtils.getStringSet(RouterPath.GoodsCenter.SP_SEARCH_HISTORY)
        mNoDataTv.setVisible(set.isEmpty())
        mDataView.setVisible(set.isNotEmpty())
        if (set.isNotEmpty()) {
            val list = mutableListOf<String>()
            list.addAll(set)
            mAdapter.setData(list)
        }
    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.mLeftIv -> finish()
            R.id.mSearchTv -> doSearch()
            R.id.mClearBtn -> {
                AppPrefsUtils.remove(RouterPath.GoodsCenter.SP_SEARCH_HISTORY)
                loadData()
            }
        }
    }

    //搜索
    private fun doSearch() {
        if (mKeywordEt.text.isNullOrEmpty()) {
            toast("请输入需要搜索的关键字")
        } else {
            val inputValue = mKeywordEt.text.toString()
            //存储关键字
            AppPrefsUtils.putStringSet(RouterPath.GoodsCenter.SP_SEARCH_HISTORY,
                    mutableSetOf(inputValue))

            //进入商品列表界面
            enterGoodsList(inputValue)
        }
    }

    /**
     * 进入商品列表界面
     */
    private fun enterGoodsList(value: String) {
        //输入不为空，进入商品列表
        ARouter.getInstance()
                .build(RouterPath.GoodsCenter.PATH_GOODS)
                .withInt(RouterPath.GoodsCenter.KEY_SEARCH_GOODS_TYPE, RouterPath.GoodsCenter.SEARCH_GOODS_TYPE_KEYWORD)
                .withString(RouterPath.GoodsCenter.KEY_GOODS_KEYWORD, value)
                .navigation()

    }

}
